package ruru.client.ui.widgets.roundedTabPanel;

import ruru.client.JSNI;
import ruru.client.ui.widgets.NoFocusMouseListenerPanel;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A tab with rounded corners for RoundedTabBar.  Mimics wowhead.com style and behavior.
 * @author zane.tian@gmail.com
 */
public class RoundedTab extends Composite implements MouseListener {
	private String tabText;
	private boolean selected;
	
	private HTML selectedTop;
	private HTML unselectedTop;
	private HTML middle;
	
	private NoFocusMouseListenerPanel fp;
	private VerticalPanel vp;
	
	public RoundedTab(String tabText, ClickListener cl) {
		vp = new VerticalPanel();
		fp = new NoFocusMouseListenerPanel(vp);
		
		this.tabText = tabText;
		this.selected = false;
		
		fp.addClickListener(cl);
	   fp.addStyleName("gwt-TabBarItem");
	   
	   unselectedTop = new HTML("<div class='rounded-tab-selected'><div></div></div>");
	   selectedTop = new HTML("<div class='rounded-tab-unselected'><div></div></div>");
	   middle = new HTML(this.tabText);
	   middle.addStyleName("morepadding");
	   middle.addStyleName("unselectedText");
	  
	   vp.add(unselectedTop);
	   vp.add(middle);
	   vp.setSpacing(0);
	   
	   JSNI.disableSelection(middle.getElement());	   
	   fp.addMouseListener(this);		
		initWidget(fp);
	}
	
	public void setSelected(boolean selected) {
		this.selected = selected;
      if(selected) {      	
      	fp.addStyleName("gwt-TabBarItem-selected");
      	middle.removeStyleName("unselectedText");
      	middle.addStyleName("selectedText");
      	vp.remove(0);
      	vp.insert(selectedTop, 0);
      } else {
      	fp.removeStyleName("gwt-TabBarItem-selected");
      	middle.removeStyleName("selectedText");
      	middle.addStyleName("unselectedText");
      	vp.remove(0);
      	vp.insert(unselectedTop, 0);
      }
	}

	public NoFocusMouseListenerPanel getFocusPanel() {
		return fp;
	}

	public void onMouseEnter(Widget sender) {
   	if(selected) {
   		
   	} else {
   		fp.addStyleName("onMouseEnter");
   		middle.addStyleName("onMouseEnter");
      	vp.remove(0);
      	vp.insert(selectedTop, 0);
   	}
	}

	public void onMouseLeave(Widget sender) {
   	if(selected) {
   		
   	} else {
   		fp.removeStyleName("onMouseEnter");
   		middle.removeStyleName("onMouseEnter");
   		vp.remove(0);
      	vp.insert(unselectedTop, 0);
   	}
   	
	}
	
	public void simluateMouseLeave() {
		fp.removeStyleName("onMouseEnter");
		middle.removeStyleName("onMouseEnter");
		vp.remove(0);
   	vp.insert(unselectedTop, 0);
	}

	public void onMouseMove(Widget sender, int x, int y) {}
	public void onMouseUp(Widget sender, int x, int y) {}
	public void onMouseDown(Widget sender, int x, int y) {}
}